#ifndef QRUNTIMEDATABASEVIEW_H
#define QRUNTIMEDATABASEVIEW_H

#include "../../lib/shared/qruntimedata.h"

#include <QTreeWidget>

#include <QAbstractTableModel>
#include <QItemDelegate>

#include <QList>
#include <QLineEdit>


class AbstractCore;
class QActionToolBar;


class NameEdit: public QLineEdit
{
    Q_OBJECT
    Q_PROPERTY(QString textValue READ value WRITE setValue USER true)
public:
    NameEdit(QWidget* parent=0);

    QString value() const;

public slots:
    void setValue(const QString & text);
private slots:
    void slotTextChanged(const QString &text);
protected:
    void markIntermediateState();
protected:
    QString     m_text;
};


class QRuntimeDatabaseView : public QTreeWidget
{
    Q_OBJECT
public:
    QRuntimeDatabaseView(AbstractCore* core,QWidget *parent=0);
    QWidget*    toolBar();
    void        addRuntime(const QRuntimeData &info);
    void        clear();
protected:
    friend class RunTableDelegate;
public slots:
    void    addNewData();
    void    removeSelect();


protected:
    void    drawRow(QPainter *painter, const QStyleOptionViewItem &options, const QModelIndex &index) const;
protected slots:
    void itemPress(const QModelIndex & index);
    void itemDoubleClicked(QTreeWidgetItem * item,int column);
    void    itemChanged (QTreeWidgetItem * item,int column);
    void    itemMoveDown();
    void    itemMoveUp();
protected:
    QColor getColor(const QString &category)const;
protected:
    QActionToolBar  *m_toolbar;
    AbstractCore    *m_core;
    QMap<QString,QTreeWidgetItem*>  m_categorys;
    QMap<QTreeWidgetItem*,QString>  m_values;
    QList<QColor>                   m_colorList;
};

class RunTableDelegate: public QItemDelegate
{
    Q_OBJECT
public:
    RunTableDelegate(QRuntimeDatabaseView *parent=0):QItemDelegate(parent),m_view(parent){}
    QWidget *	createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    void	paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    void	updateEditorGeometry ( QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    void        setEditorData ( QWidget * editor, const QModelIndex & index );
    void        setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    QSize       sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
protected:
    QRuntimeDatabaseView    *m_view;
};

#endif // QRUNTIMEDATABASEVIEW_H
